package org.jeecg.modules.appuser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pdd.pop.sdk.common.util.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.appuser.entity.TbMnCollect;
import org.jeecg.modules.appuser.entity.TbUserAlipay;
import org.jeecg.modules.appuser.mapper.TbUserAlipayMapper;
import org.jeecg.modules.appuser.service.ITbUserAlipayService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Description: 用户支付宝账户
 * @Author: jeecg-boot
 * @Date: 2020-05-18
 * @Version: V1.0
 */
@Service
public class TbUserAlipayServiceImpl extends ServiceImpl<TbUserAlipayMapper, TbUserAlipay> implements ITbUserAlipayService {

    @Override
    public Result<?> cashAlipayBinding(TbUserAlipay tbUserAlipay) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", tbUserAlipay.getUserId());
        TbUserAlipay tbUserAlipay1 = this.getOne(queryWrapper);
        if (null != tbUserAlipay1) {
            return Result.ok("已经绑定");
        }
        if (StringUtils.isEmpty(tbUserAlipay.getAlipayAccount())) {
            return Result.error("参数错误");
        }
        if (StringUtils.isEmpty(tbUserAlipay.getRealName())) {
            return Result.error("参数错误");
        }
        if (StringUtils.isEmpty(tbUserAlipay.getUserId())) {
            return Result.error("参数错误");
        }
        tbUserAlipay.setState(1);
        if (this.save(tbUserAlipay)) {
            return Result.ok("绑定成功");
        }
        return Result.ok("绑定失败");
    }

    @Override
    public Result<?> uncashAlipayBinding(String user_id) {
        if(StringUtils.isEmpty(user_id)){
            return Result.error("参数错误");
        }
        TbUserAlipay tbUserAlipay = this.getByUserId(user_id);
        if (null != tbUserAlipay) {
            this.removeById(tbUserAlipay.getId());
            return Result.ok("解绑成功");
        }
        return Result.ok("解绑失败");
    }

    @Override
    public TbUserAlipay getByUserId(String userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        TbUserAlipay tbUserAlipay = this.getOne(queryWrapper);
        return tbUserAlipay;
    }

    @Override
    public IPage<TbMnCollect> pageAndUserData(Page<TbMnCollect> page, TbUserAlipay tbUserAlipay) {
        return page.setRecords(this.baseMapper.pageAndUserData(page, tbUserAlipay));
    }
}
